from selenium import webdriver
import re
from selenium.webdriver.common.by import By
import time
# 创建浏览器驱动对象
driver = webdriver.Chrome()
driver.get()
agency = '000000'
username = '0000000'
password = '0000'
codeVaild = input("验证码输入:")
agency_input = driver.find_element(By.XPATH,'//*[@id="inputunitname"]')
username_input = driver.find_element(By.XPATH,'//*[@id="uname"]')
password_input = driver.find_element(By.XPATH,'//*[@id="password"]')
vercode_input = driver.find_element(By.XPATH,'//*[@id="vercode"]')
agency_input.send_keys(agency)
username_input.send_keys(username)
password_input.send_keys(password)
vercode_input.send_keys(codeVaild)
time.sleep(4)
button_login = driver.find_element(By.ID,'loginBtn')
button_login.click()
time.sleep(1)
# 学生连接
url_student = ''
driver.get(url_student)
for item in range(4):
    driver.refresh()
    html = driver.page_source
    # 使用正则表达式匹配第一个<input>元素的名称
    pattern = r'placeholder="0-\d+\.\d+"'
    matches = re.findall(pattern, html)
    dict_sorce = []
    if matches:
        for match in matches:
            num_str = re.search(r'placeholder="0-(\d+\.\d+)"', match).group(1)
            num = float(num_str)
            dict_sorce.append({match: num_str})
    else:
        break
    # 去重
    original_list = dict_sorce
    # 用于存储已经出现过的键对应的值
    seen_values = set()
    # 用于存储去重后的结果
    result = []
    for d in original_list:
        # 获取字典中的键和值
        key, value = next(iter(d.items()))
        # 如果值尚未出现过，则将其加入到结果列表中，并添加到集合中
        if value not in seen_values:
            seen_values.add(value)
            result.append({key: value})
    for item in result:
        placeholder_str = list(item.keys())[0]
        print(f'//input[@{placeholder_str}]', item[placeholder_str])
        getElementScore_sets = driver.find_elements(By.XPATH, f'//input[@{placeholder_str}]')
        for item_element in getElementScore_sets:
            item_element.send_keys(item[placeholder_str])
        time.sleep(1)
    submit_button = driver.find_element(By.XPATH, '//a[@onclick="pigai(0);"]')
    time.sleep(2)
    submit_button.click()
driver.close()